<?php

namespace App\Admin\Controllers;

use App\Models\Menu;
use App\Models\Slide;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class SlideController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            if ($menuId = request('menuId')) {
                $menu = Menu::find($menuId);
                session(['menu' => $menu]);
            } else {
                session(['menu' => null]);
                $menu=null;
            }
            $content->header('幻灯片列表');
            $content->description('幻灯片列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('幻灯片编辑');
            $content->description('幻灯片编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('幻灯片新增');
            $content->description('幻灯片新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Slide::class, function (Grid $grid) {

            if ($menu = session('menu')) {
                $grid->model()->where('menu_id', $menu->id);
                $grid->disableExport();
                $grid->disableFilter();
            }
            $grid->id('ID')->sortable();
            $grid->column('title','名称');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Slide::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->text('title','名称');
            $form->text('url','url');
            $form->switch('isShow', '是否显示')->states(config('admin.status'));
//            $form->select('menu_id','模块')->options(function ($ids) {
//                $data = Menu::find($ids);
//                return $data ? $data->pluck('title', 'id') : null;
//            })->ajax('/admin/menu/get');
            $form->image('pic','图片')->uniqueName();
            if ($menu = session('menu')) {
                $form->hidden('menu_id')->default($menu->id);
//                $form->setAction("/admin/slide?menuId={$menu->id}");
                $form->saved(function (Form $form) use ($menu) {
                    return redirect("admin/slide?menuId={$menu->id}");
                });
            }
        });
    }
}
